# -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
#    $Id$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from osv import fields
from osv import osv
import wizard
import netsvc
import pooler
import time
from osv.orm import browse_record

class wiz_project_quotation_open(wizard.interface):
    def _open_quotation(self, cr, uid, data, context):
        pool_obj = pooler.get_pool(cr.dbname)
        prj_ids = ','.join(map(str,data['ids']))
        cr.execute("Select\
                        id \
                    from \
                        sale_order so\
                    where\
                        project_e_id in (%s)\
                        or project_m_id in (%s)" % (prj_ids,prj_ids))
        quotation_ids=[]
        for q_id in cr.fetchall():
            quotation_ids.append(q_id)
        quotation_ids= ','.join(map(str,quotation_ids))
                
        value = {
            'domain': "[('id', 'in',["+quotation_ids+"])]",
            'name': 'Quotation',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'sale.order',
            'type': 'ir.actions.act_window',
        }
        return value

    states = {
        'init' : {
            'actions' : [],
            'result' : {'type':'action', 'action':_open_quotation, 'state':'end'}
        }
    }
wiz_project_quotation_open('project.quotation.open')

class wiz_project_cfp_open(wizard.interface):
    def _open_cfp(self, cr, uid, data, context):
        pool_obj = pooler.get_pool(cr.dbname)
        prj_ids = ','.join(map(str,data['ids']))
        
        cr.execute("Select\
                        id \
                    from \
                        kdvn_payment_from_client \
                    where\
                        contract_id in \
                            (Select\
                                    contract_id \
                            from \
                                    sale_order \
                            where \
                                project_e_id in (%s) or project_m_id in (%s))" % (prj_ids,prj_ids))
        cfp_ids = []
        #quotation_ids=[]
        for cfp_id in cr.fetchall():
            cfp_ids.append(cfp_id)
        cfp_ids= ','.join(map(str,cfp_ids))
        value = {
            'domain': "[('id', 'in',["+cfp_ids+"])]",
            'name': 'Payment from Client',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'kdvn.payment.from.client',
            'type': 'ir.actions.act_window',
        }
        return value

    states = {
        'init' : {
            'actions' : [],
            'result' : {'type':'action', 'action':_open_cfp, 'state':'end'}
        }
    }
wiz_project_cfp_open('project.cfp.open')

class wiz_contract_quotation_open(wizard.interface):
    def _open_quotation(self, cr, uid, data, context):
        pool_obj = pooler.get_pool(cr.dbname)
        ctc_ids = ','.join(map(str,data['ids']))
        cr.execute("Select\
                        id \
                    from \
                        sale_order so\
                    where\
                        contract_id in (%s)" % (ctc_ids))
        quotation_ids=[]
        for q_id in cr.fetchall():
            quotation_ids.append(q_id)
        quotation_ids= ','.join(map(str,quotation_ids))
        
        value = {
            'domain': "[('id', 'in',["+quotation_ids+"])]",
            'name': 'Quotation',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'sale.order',
            'type': 'ir.actions.act_window',
        }
        return value

    states = {
        'init' : {
            'actions' : [],
            'result' : {'type':'action', 'action':_open_quotation, 'state':'end'}
        }
    }
wiz_contract_quotation_open('contract.quotation.open')


class wiz_contract_contract_client_report_open(wizard.interface):
    def _open_contract_to_client_report(self, cr, uid, data, context):
        pool_obj = pooler.get_pool(cr.dbname)
        ctc_ids = ','.join(map(str,data['ids']))
        #quotation_ids = cr.fetchall()
        value = {
            'domain': "[('contract_id', 'in',["+ctc_ids+"])]",
            'name': 'Contract to Client Report',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'kdvn.report.contract.to.client',
            'type': 'ir.actions.act_window',
        }
        return value

    states = {
        'init' : {
            'actions' : [],
            'result' : {'type':'action', 'action':_open_contract_to_client_report, 'state':'end'}
        }
    }
wiz_contract_contract_client_report_open('contract.ctoclient.report.open')
